﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LsKeyboardCore
{
    public partial class NumericKeyboard : Form
    {
        public NumericKeyboard()
        {
            InitializeComponent();
        }

        private void NumericKeyboard_Load(object sender, EventArgs e)
        {
            this.BackColor = KeyBoardAppearance.KeyBoardFormBackColor;
            InitLocation();
        }
        /// <summary>
        /// 初始化位置
        /// </summary>
        public void InitLocation()
        {
            Rectangle rectangle = Screen.PrimaryScreen.Bounds;
            int x = rectangle.Width / 2 - this.Width / 2;
            int y = rectangle.Height - this.Height - 80;
            this.Location = new Point(x, y);
        }
        /// <summary>
        /// 右侧位置
        /// </summary>
        public void RightLocation()
        {
            Rectangle rectangle = Screen.PrimaryScreen.Bounds;
            int x = rectangle.Width- this.Width;
            int y = rectangle.Height - this.Height - 80;
            this.Location = new Point(x, y);
        }
        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void kClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 实现光标在选中目标上
        /// </summary>
        protected override CreateParams CreateParams
        {
            // CreateParams 在 System.Windows.Forms.Controls 命名空间下 
            get
            {
                const int WS_EX_NOACTIVATE = 0x08000000;
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= WS_EX_NOACTIVATE;
                return cp;
            }
        }

        #region 控制窗体拖动
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        private bool isMouseDown = false;
        /// <summary>
        /// 鼠标按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NumericKeyboard_MouseDown(object sender, MouseEventArgs e)
        {
            isMouseDown = true;
        }
        /// <summary>
        /// 鼠标弹起
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NumericKeyboard_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
        }
        /// <summary>
        /// 鼠标移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NumericKeyboard_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                if (e.Button == MouseButtons.Left)
                {
                    Win32Api.ReleaseCapture();

                    Win32Api.SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
                }
            }
        }
        #endregion
        /// <summary>
        /// 全键盘
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void keyFull_Click(object sender, EventArgs e)
        {
            FullKeyboardForm fullKeyboardForm = new FullKeyboardForm(this);
            fullKeyboardForm.Show();
            this.Hide();
        }
        /// <summary>
        /// 键盘事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void key1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (sender is LabelEx)
                {
                    LabelEx lb = sender as LabelEx;
                    string key = Convert.ToString(lb.Tag);
                    if (!string.IsNullOrEmpty(key))
                    {
                        SendKeys.Send(key);
                    }
                }
            }
            catch(Exception ex)
            {

            }
        }
        /// <summary>
        /// 双击隐藏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panelTop_DoubleClick(object sender, EventArgs e)
        {
            if (this.Height >= 512)
            {
                this.Size = new Size(100, 64);
                RightLocation();
            }
            else
            {
                this.Size = new Size(450, 512);
                InitLocation();
            }
        }
        /// <summary>
        /// 大小调整
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panelTop_SizeChanged(object sender, EventArgs e)
        {
            if (kClose.IsHandleCreated)
            {
                kClose.Location = new Point(panelTop.Width - kClose.Width - 8, 8);
            }
        }

        private void keySwitch_Click(object sender, EventArgs e)
        {
            string key = Convert.ToString(keySwitch.Tag);
            if (!string.IsNullOrEmpty(key))
            {
                SendKeys.Send(key);
            }
        }
    }
}
